<template>
  <div style="line-height: 0;">
    <div style="margin-top: 15px; margin-bottom: 15px; line-height: 0; text-align: left;">
      <el-row>
        <label style="margin-left: 10px;">姓名：</label>
        <el-input style="width: 200px;" prefix-icon="el-icon-search" v-model="params.username" placeholder="请输入姓名"></el-input>
          &nbsp;&nbsp;&nbsp;
        <el-button type="warning" round style="margin-left: 10px;" @click="findUser">查询</el-button>
        <el-button type="warning" round style="margin-left: 10px;" @click="reset">重置</el-button>
        <el-button type="warning" round style="margin-left: 10px;" @click="add">新增</el-button>
      </el-row>
    </div>
    <div >
      <el-table
          :data="tableData"
          border
          style="width: 100%">
        <el-table-column prop="user_id" v-if="false"></el-table-column>
        <el-table-column prop="account" label="账号" width="180"></el-table-column>
        <el-table-column prop="username" label="姓名" width="180"></el-table-column>
        <el-table-column prop="email" label="电子邮箱"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click="edit(scope.row)">编辑</el-button>
            <el-popconfirm title="确定要删除吗?" @confirm="del(scope.row)">
              <el-button slot="reference" type="danger" style="margin-left: 5px;">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block" style="text-align: center;">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="params.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <!--新增和编辑共用-->
    <div >
      <el-dialog title="用户信息" :visible.sync="dialogFormVisible" >
        <el-form :model="form" style="text-align: center;">
          <el-form-item label="账号" :label-width="formLabelWidth">
            <el-input v-model="form.account" autocomplete="off" style="width: 80%;"></el-input>
          </el-form-item>
          <el-form-item label="姓名" :label-width="formLabelWidth">
            <el-input v-model="form.username" autocomplete="off" style="width: 80%;"></el-input>
          </el-form-item>
          <el-form-item label="电子邮箱" :label-width="formLabelWidth">
            <el-input v-model="form.email" autocomplete="off" style="width: 80%;"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer" >
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </div>
      </el-dialog>
    </div>


  </div>

</template>
<script>
import {userInfo} from "@/api/user";
import request from "@/utils/request";
export default {
  name:'userinfoview',
  data() {
    return {
      params: {
        username:'',
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      total: 0,
      formLabelWidth: '190px',
      dialogFormVisible: false,
      form : {}
    }
  },
  //在页面加载的时候，做一些事情
  created() {
    this.findUser();
  },
  //定义一些页面上触发事件调用的方法
  methods:{
    findUser(){
      userInfo(this.params).then(res => {
        if (res.code === '0'){
          this.tableData = res.data.list;
          this.total = res.data.total;
        } else{
          this.$message({
            message: res.msg,
            type: 'error'
          })
        }
      })
    },
    reset(){
      this.params = {
        pageNum: 1,
        pageSize: 10,
        username: '',
        phone: '',
      }
      this.findUser();
    },
    handleSizeChange(pageSize){
      this.params.pageSize = pageSize;
      this.findUser();
    },
    handleCurrentChange(pageNum){
      this.params.pageNum = pageNum;
      this.findUser();
    },
    add(){
      this.form = {}
      this.dialogFormVisible = true;
    },
    edit(obj){
      this.form = obj;
      this.dialogFormVisible = true;
    },

    del(user) {
      request.delete(`/user/del/${user.userId}`).then(res => {
        if (res.code === '0') {
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.findUser();
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    submit(){
      request.post("/user/addedit",this.form).then(res => {
        if(res.code === '0'){
          this.$message.success("操作成功")
        }
        else{
          this.$message.error(res.msg)
        }
      })
      this.dialogFormVisible = false;
      this.findUser();
    }
  }
}
</script>